﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace MergeShapeFileLib
{
    public class MergeShapeFiles
    {
        List<string> filesPath;
        MergeTwoShapeFile mergeTwoShapeFile;
        private string dfile;
        private string targetFile;
        private static int index = 0;
        public static int temp = 1;

        public MergeShapeFiles(List<string> filesPath, string dfile)
        {
            this.filesPath = new List<string>();
            this.filesPath = filesPath;

            this.dfile = dfile;
            this.targetFile = dfile;
            this.mergeTwoShapeFile = new MergeTwoShapeFile();
        }

        public void mergeFiles()
        {
            if (filesPath.Count == 1 || filesPath.Count == 0)
            {
                return;
            }
            else if (filesPath.Count == 2)
            {
                mergeTwoShapeFile.mergeTwoShapeFile(filesPath[index], filesPath[++ index], dfile);
                return;
            }
            else
            {
                if (index == 0)
                {
                    mergeTwoShapeFile.mergeTwoShapeFile(filesPath[index], filesPath[++ index], dfile);
                    mergeTwoShapeFile.mergeTwoShapeFile(dfile, filesPath[++ index], dfile + temp.ToString());
                    //删除临时文件
                    File.Delete(dfile);
                    //
                    dfile = dfile + temp.ToString();
                    temp ++;
                }
                else
                {
                    mergeTwoShapeFile.mergeTwoShapeFile(dfile, filesPath[++ index], dfile+temp.ToString());
                    //删除临时文件
                    File.Delete(dfile);
                    //
                    dfile = dfile + temp.ToString();
                    temp++;
                }
            }

            if (index == (filesPath.Count - 1))
            {
                File.Move(dfile,targetFile);
                return;
            }
            else
            {
                mergeFiles();
            }
        }

        public static int getProValue()
        {
            return temp;
        }
    }
}
